home *** CD-ROM | disk | FTP | other *** search
- /* Copyright (C) 2003 Jeff Gilpin (jeg@dejazzd.com),
- Tom Parker (tom@carrott.org)
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-
- In addition, as a special exception, Tom Parker and Matthias Münch give
- permission to link the code of this program with a TCP stack of your
- choice, any official MUI libraries or classes and any custom MUI classes
- that should be necessary for the operation of this program. This
- exception also gives you permission to distribute linked combinations
- including this software with any of the before-mentioned libraries and
- classes. You must obey the GNU General Public License in all respects for
- all of the code used other than that provided by the before-mentioned
- libraries and classes. As part of this exception you are obliged to
- follow the license terms of the before-mentioned libraries, this license
- does not compel you to follow those terms, but if you do not then you may
- not link with those libraries. If you modify this file, you may extend
- this exception to your version of the file, but you are not obligated to
- do so. If you do not wish to do so, delete this exception statement from
- your version.
- */
- /*
- ** Functions to load external ILBM Images for NList
- */
-
- #include <libraries/iffparse.h>
- #include <datatypes/pictureclass.h>
- #include <clib/iffparse_protos.h>
- #include "common.h"
- #include "muihelp.h"
-
-
- void cleanup();
- void closeup();
- BOOL unpack(UBYTE *source, UBYTE *dest, int srcsize, int destsize);
-
- muidefpix *loadPix(char *filename) {
- struct BitMapHeader *bmhdr;
- struct StoredProperty *property;
- struct IFFHandle *jiff;
- muidefpix *muipix;
- LONG size, newsize;
- int width, count;
-
- if (!(jiff = AllocIFF())) {
- closeup(jiff);
- return NULL;
- }
- if (!(jiff->iff_Stream = Open(filename, MODE_OLDFILE))) {
- closeup(jiff);
- return NULL;
- }
- InitIFFasDOS(jiff);
-
- if (OpenIFF(jiff, IFFF_READ) ||
- PropChunk(jiff, ID_ILBM, ID_BMHD) ||
- PropChunk(jiff, ID_ILBM, ID_CMAP) ||
- PropChunk(jiff, ID_ILBM, ID_BODY) ||
- StopOnExit(jiff, ID_ILBM, ID_BODY)) {
- closeup(jiff);
- return NULL;
- }
- if (ParseIFF(jiff, IFFPARSE_SCAN) != -2) { closeup(jiff); return NULL; }
-
- if (!(property = FindProp(jiff, ID_ILBM, ID_BMHD))) { closeup(jiff); return NULL; }
- if (!(muipix = (muidefpix *) calloc(1, sizeof(muidefpix)))) { closeup(jiff); return NULL; }
- if (!(bmhdr = property->sp_Data)) { cleanup(muipix, jiff); return NULL; }
- muipix->w = bmhdr->bmh_Width;
- if (muipix->w < 2) { cleanup(muipix, jiff); return NULL; }
- muipix->h = bmhdr->bmh_Height;
- if (muipix->h < 2) { cleanup(muipix, jiff); return NULL; }
- muipix->d = bmhdr->bmh_Depth;
- if ((muipix->d < 1) || (muipix->d > 4)) { cleanup(muipix, jiff); return NULL; }
- muipix->comp = bmhdr->bmh_Compression;
- if (muipix->comp > 1) { cleanup(muipix, jiff); return NULL; }
- muipix->mask = bmhdr->bmh_Masking;
-
- if (!(property = FindProp(jiff, ID_ILBM, ID_CMAP))) { cleanup(muipix, jiff); return NULL; }
- size = property->sp_Size;
- if (!(muipix->colors = (void *) calloc((size_t)(size + 2), sizeof(ULONG)))) { cleanup(muipix, jiff); return NULL; }
- for (count = 0; count < size; count++) {
- UBYTE rgb = ((UBYTE *)property->sp_Data)[count];
- muipix->colors[count] = (rgb << 24) | (rgb << 16) | (rgb << 8) | rgb;
- }
-
- if (!(property = FindProp(jiff, ID_ILBM, ID_BODY))) { cleanup(muipix, jiff); return NULL; }
- size = property->sp_Size;
- if (muipix->comp == 0) {
- if (!(muipix->body = (void *) calloc((size_t)(size + 2), 1))) {
- cleanup(muipix, jiff);
- return NULL;
- }
- memcpy(muipix->body, property->sp_Data, size);
- closeup(jiff);
- return muipix;
- }
- width = muipix->w;
- width = (width >> 3) + ((width & 7) > 0);
- width = width + (width & 1);
- newsize = width * muipix->h * muipix->d;
- if (!(muipix->body = (void *) calloc((size_t)(newsize + 2), 1))) { cleanup(muipix, jiff); return NULL; }
- if (!(unpack(property->sp_Data, muipix->body, size, newsize))) { cleanup(muipix, jiff); return NULL; }
- muipix->comp = 0;
- closeup(jiff);
- return muipix;
- }
-
- BOOL unpack(UBYTE *source, UBYTE *dest, int srcsize, int destsize) {
- int src, dst, count;
- UBYTE temp;
- dst = 0;
- src = 0;
- while (src < srcsize) {
- count = (BYTE)source[src++];
- if (count >= 0) {
- count += 1;
- do {
- dest[dst++] = source[src++];
- } while ((--count > 0) && (dst < destsize) & (src < srcsize));
- } else if (count != -128) {
- count = -count + 1;
- temp = source[src++];
- do {
- dest[dst++] = temp;
- } while ((--count > 0) & (dst < destsize));
- }
- if (dst > destsize) {
- return FALSE;
- }
- }
- return TRUE;
- }
-
- void cleanup(muidefpix *muipix, struct IFFHandle *jiff) {
- closeup(jiff);
- if (muipix->body) {
- free(muipix->body);
- }
- if (muipix->colors) {
- free(muipix->colors);
- }
- if (muipix) {
- free(muipix);
- }
- return;
- }
-
- void closeup(struct IFFHandle *jiff) {
- if (jiff) {
- CloseIFF(jiff);
- if (jiff->iff_Stream)
- Close(jiff->iff_Stream);
- FreeIFF(jiff);
- }
- return;
- }
-